package com.listeningframework.boot.autoconfigure.template.freemarker.overrides;

import java.util.HashMap;

import com.listeningframework.boot.autoconfigure.template.freemarker.ListeningFreeMarkerProperties;

import freemarker.template.SimpleHash;

public abstract class ListeningFreemarkerSimpleHash extends SimpleHash {

    /**  */
    private static final long serialVersionUID = -1244384920465672716L;

    public ListeningFreemarkerSimpleHash() {
        super(new HashMap<>(), null);
    }

    public abstract void execute();

    public abstract String getVariable();

    /**
     * 添加标签
     *
     * @param tagClz 标签类
     * @param prefix 前缀
     * @param properties freemarker配置参数
     */
    protected void addTag(Class<? extends ListeningStandardFreemarkerTagSupport> tagClz, String prefix,
                          ListeningFreeMarkerProperties properties) {
        try {
            ListeningStandardFreemarkerTagSupport item = tagClz.newInstance();
            if (properties != null) {
                item.setListeningProperties(properties);
            }
            put(prefix, item);
        } catch (Exception e) {
            throw new RuntimeException("Tag[" + prefix + "] Add Fail", e);
        }
    }

    /**
     * 添加标签
     *
     * @param tagClz 标签类
     * @param prefix 前缀
     */
    protected void addTag(Class<? extends ListeningStandardFreemarkerTagSupport> tagClz, String prefix) {
        addTag(tagClz, prefix, null);
    }
}
